package com.hjc.service.impl;

import com.alibaba.fastjson.JSON;
import com.hjc.entity.Auth;
import com.hjc.mapper.AuthMapper;
import com.hjc.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class AuthServiceImpl implements AuthService {
    @Autowired
    private AuthMapper authMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 根据用户（角色）id查询权限(菜单)树的业务方法
     * @param userId
     * @return
     */
    @Override
    public List<Auth> findAuthTreeByUserId(int userId) {
        //先查看redis缓存有无权限树,先从redis中查询缓存,查到的是权限(菜单)树List<Auth>转的json串
        String authTreeListJson = redisTemplate.opsForValue().get("authTree:" + userId);//redis中键的形式：authTree:id
        if (StringUtils.hasText(authTreeListJson)) {
            //不为空表示有缓存，转换格式后直接返回
            List<Auth> authTreeList = JSON.parseArray(authTreeListJson, Auth.class);
            return authTreeList;
        }
        //若没有查到，则从数据库中查找权限集合
        List<Auth> allAuthList = authMapper.findAllAuthByUserId(userId);
        //利用递归函数，把权限数组转换为权限树
        List<Auth> authTree = authListToAuthTree(allAuthList,0);
        //存入redis
        redisTemplate.opsForValue().set("authTree:" + userId, JSON.toJSONString(authTree));
        return authTree;
    }

    /**
     * 将所有权限(菜单)转成权限(菜单)树的递归算法
     * @param allAuthList
     * @param parentId
     * @return
     */
    private List<Auth> authListToAuthTree(List<Auth> allAuthList,int parentId) {
        //获取父权限(菜单)id为参数parentId的所有权限(菜单)
        //【parentId最初为0,即最初查的是所有一级权限(菜单)】
        List<Auth> authList = new ArrayList<>();
        for (Auth auth : allAuthList) {
            if (auth.getParentId() == parentId) {
                authList.add(auth);
            }
        }
        //查询List<Auth> authList中每个权限(菜单)的所有子级权限(菜单)
        for (Auth auth : authList) {
            List<Auth> childAuthList = authListToAuthTree(allAuthList, auth.getAuthId());
            auth.setChildAuth(childAuthList);
        }
        return authList;
    }

    @Override
    public List<Auth> findAllAuth() {
        //先查看redis缓存有无权限树,先从redis中查询缓存,查到的是权限(菜单)树List<Auth>转的json串
        String authTreeListJson = redisTemplate.opsForValue().get("all:authTree");//redis中键的形式：all:authTree
        if (StringUtils.hasText(authTreeListJson)) {
            //不为空表示有缓存，转换格式后直接返回
            List<Auth> authTreeList = JSON.parseArray(authTreeListJson, Auth.class);
            return authTreeList;
        }
        //若没有查到，则从数据库中查找权限集合
        List<Auth> allAuthList = authMapper.selectAllAuth();
        //利用递归函数，把权限数组转换为权限树
        List<Auth> authTree = authListToAuthTree(allAuthList,0);
        //存入redis
        redisTemplate.opsForValue().set("all:authTree", JSON.toJSONString(authTree));
        return authTree;
    }




}
